package ua.epam.java_bravo.controller;

import ua.epam.java_bravo.controller.servletCommand.AddrFormCommand;
import ua.epam.java_bravo.controller.servletCommand.ICommand;
import ua.epam.java_bravo.controller.servletCommand.LoginCommand;
import ua.epam.java_bravo.controller.servletCommand.NoCommand;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Linderhof
 * Date: 15.12.2010
 * Time: 13:21:27
 */
public class RequestHelper {
    private static RequestHelper instance = null;

    HashMap<String, ICommand> commands =
            new HashMap<String, ICommand>();

    /**
     * Initializes class with all possible servlet commands
     */
    private RequestHelper() {
        commands.put("addrForm", new AddrFormCommand());
        commands.put("login", new LoginCommand());
        //todo add more commands
    }

    /**
     * Extracts servlet command from request
     *
     * @param request
     * @return
     */
    public ICommand getCommand(HttpServletRequest request) {
       String action = request.getParameter("command");

       ICommand command = commands.get(action);
       if (command == null) {
           command = new NoCommand();
       }

        return command;
    }

    /**
     * Singleton
     * 
     * @return
     */
    public static RequestHelper getInstance() {
        if (instance == null) {
            instance = new RequestHelper();
        }
        return instance;
    }
}
